package com.ssyan.demomongo;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.ssyan.demomongo.entity.User;
import com.ssyan.demomongo.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.regex.Pattern;

@SpringBootTest
class DemomongoApplicationTests1 {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserRepository userRepository;

    //添加操作
    @Test
    void create() {
        User user = new User();
        user.setAge(20);
        user.setName("test4");
        user.setEmail("123@qq.com");
        User u = userRepository.save(user);
        System.out.println(u);
    }

    //查询所有记录
    @Test
    void findAll() {
        List<User> all =
                userRepository.findAll();
        System.out.println(all);
    }

    //根据ID查询
    @Test
    void findId() {
        User user = userRepository.findById("60eda3bfc48d5e7fed211226").get();
        System.out.println(user);
    }

    //条件查询
    @Test
    void findUserByList() {
        // name=test and  age=20
        User user = new User();
        user.setAge(20);
        user.setName("test4");
        Example<User> userExample = Example.of(user);
        List<User> all = userRepository.findAll(userExample);
        System.out.println(all);
    }

    //模糊查询
    @Test
    void findLikeUserList(){

        //设置模糊查询的匹配规则
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase();

        User user = new User();
        user.setAge(20);
        user.setName("est");
        Example<User> userExample = Example.of(user,matcher);
        List<User> all = userRepository.findAll(userExample);
        System.out.println(all);
    }

    //分页查询
    @Test
    void findPageUserList(){

        //分页
        //0代表第一页
        Pageable pageable = PageRequest.of(0, 3);

        User user = new User();
        user.setAge(20);
        user.setName("test4");
        Example<User> userExample = Example.of(user);
        //添加断点，查询page中的值
        Page<User> page = userRepository.findAll(userExample, pageable);

        System.out.println(page);
    }

    //修改
    @Test
    void updateUser() {

        //根据id查询
        User user = userRepository.findById("60eda3bfc48d5e7fed211226").get();

        //设置修改值
        user.setName("test_2");
        user.setAge(100);
        user.setEmail("0000@qq.com");

        //调用方法进行修改
        //判断新增和修改的底层：是否传入id值
        User save = userRepository.save(user);
        System.out.println(save);
    }

    //删除
    @Test
    void deleteUser(){
        userRepository.deleteById("60eda3bfc48d5e7fed211226");
    }
}
